package com.bincloud.vue.admin.web.controller.ums;


import com.alibaba.fastjson.JSON;
import com.bincloud.vue.admin.common.model.dto.response.BasePageResDTO;
import com.bincloud.vue.admin.common.model.dto.response.BaseResDTO;
import com.bincloud.vue.admin.common.util.ResponseUtils;
import com.bincloud.vue.admin.web.dto.ums.user.UmsUserPageReqDTO;
import com.bincloud.vue.admin.web.dto.ums.user.UmsUserPageResDTO;
import com.bincloud.vue.admin.web.dto.ums.user.UmsUserReqDTO;
import com.bincloud.vue.admin.web.dto.ums.user.UmsUserSaveReqDTO;
import com.bincloud.vue.admin.web.service.ums.IUmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Api(tags = "用户管理相关接口")
@RestController
@RequestMapping("/admin/ums/user")
@Slf4j
public class UmsUserController {

    @Resource
    private IUmsUserService umsUserService;

    @PreAuthorize(value = "hasAuthority('admin:ums:user')")
    @ApiOperation(value = "用户分页查询", produces = "application/json")
    @PostMapping(value = "/page")
    public BaseResDTO<BasePageResDTO<List<UmsUserPageResDTO>>> page(@Validated @RequestBody UmsUserPageReqDTO reqDTO) {
        log.info("用户分页查询,入参:{}", JSON.toJSONString(reqDTO));
        BasePageResDTO<List<UmsUserPageResDTO>> resDTO = umsUserService.pageUser(reqDTO);
        return ResponseUtils.ok(resDTO);
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:user')")
    @ApiOperation(value = "用户新增", produces = "application/json")
    @PostMapping(value = "/save")
    public BaseResDTO<Boolean> save(@Validated @RequestBody UmsUserSaveReqDTO reqDTO) {
        log.info("用户新增,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsUserService.saveUser(reqDTO);
        log.info("用户新增,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:user')")
    @ApiOperation(value = "用户锁定", produces = "application/json")
    @PostMapping(value = "/lock")
    public BaseResDTO<Boolean> lock(@Validated @RequestBody UmsUserReqDTO reqDTO) {
        log.info("用户锁定,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsUserService.lockUser(reqDTO);
        log.info("用户锁定,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:user')")
    @ApiOperation(value = "用户解除锁定", produces = "application/json")
    @PostMapping(value = "/unlock")
    public BaseResDTO<Boolean> unlock(@Validated @RequestBody UmsUserReqDTO reqDTO) {
        log.info("用户解除锁定,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsUserService.unlockUser(reqDTO);
        log.info("用户解除锁定,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:user')")
    @ApiOperation(value = "用户删除", produces = "application/json")
    @PostMapping(value = "/remove")
    public BaseResDTO<Boolean> remove(@Validated @RequestBody UmsUserReqDTO reqDTO) {
        log.info("用户删除,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsUserService.removeUser(reqDTO);
        log.info("用户删除,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:user')")
    @ApiOperation(value = "用户重置", produces = "application/json")
    @PostMapping(value = "/reset")
    public BaseResDTO<Boolean> reset(@Validated @RequestBody UmsUserReqDTO reqDTO) {
        log.info("用户重置,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsUserService.resetUser(reqDTO);
        log.info("用户重置,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }


}

